package servlets;

import services.ManagerService;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class loginServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String id = request.getParameter("id");
        String pwd = request.getParameter("password");
        String userType = request.getParameter("userType");

        PrintWriter out = response.getWriter();
        ManagerService maj = new ManagerService();

        if (maj.getPasswordById(id).equals(pwd)) {
            switch (userType) {
                case "true":
                    if (userType.equals(maj.getIsRootById(id))) {
                        HttpSession session = request.getSession(true);
                        session.setAttribute("id", id);
                        session.setAttribute("isRoot", userType);
                        session.setAttribute("toGo", "./manager_subFrame.jsp");
                        out.write("./main_frame.jsp");
                    } else
                        out.write("false");
                    break;
                case "false":
                    if (userType.equals(maj.getIsRootById(id))) {
                        HttpSession session = request.getSession(true);
                        session.setAttribute("id", id);
                        session.setAttribute("isRoot", userType);
                        session.setAttribute("toGo", "./stu_subFrame.jsp");
                        out.write("main_frame.jsp");
                    } else
                        out.write("false");
                    break;
                default:
                    out.write("false");
                    break;
            }
        }
        else
            out.write("false");
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();
    }
}
